﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

namespace ShootLife2D.GUI.Controls
{
    public class AnimatedImageBox : ImageBox
    {
        public Vector2 FrameSize;
        public int Frames;
        private Rectangle _currentFrame;
        private int _currentIntFrame = 0;
        public float UpdateTime = 0.03f;
        private float _timeLeft;
        public AnimatedImageBox(Vector2 frameSize,Texture2D image, Manager manager)
            : base(manager)
        {
            Image = image;
            FrameSize = frameSize;
            _currentFrame = new Rectangle(0,0,(int)FrameSize.X,(int)FrameSize.Y);
        }
        protected override void InitSkin()
        {
            base.InitSkin();

            // We specify what skin this control uses. We use standard tooltip background here.
            //Skin = new SkinControl(Manager.Skin.Controls["ImageBox"]);
        }
        protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
        {
            //base.DrawControl(renderer, rect, gameTime);
            //SkinLayer l = Skin.Layers[0];

            // We render background of the tooltip
            //renderer.DrawLayer(this, l, rect);

            Rectangle rc1 = Rectangle.Empty;
            if (Image != null)
            {
                // Now we draw image in the left top corner of the tooltip
                //rc1 = AbsoluteRect;
                rc1 = DrawingRect;
                //rc1 = new Rectangle(5, Height+20, Width, Height);
                //renderer.Draw(Image, rc1, Color.White);
                renderer.Draw(Image, rc1, _currentFrame, Color.White);
            }

            // Text is rendered next to the image
            //rect = new Rectangle(rc1.Right, rect.Top + 4, rect.Width, rect.Height);

            //// We alter text alignment from the default skin
            //l.Text.Alignment = Alignment.TopLeft;
            //renderer.DrawString(this, l, Text, rect, true);
        }
        protected override void Update(GameTime gameTime)
        {
            _timeLeft += gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
            if (_timeLeft >= UpdateTime)
            {
                _timeLeft = 0;
                int yFrame = _currentIntFrame / (Image.Width / (int)FrameSize.X);
                int xFrame = _currentIntFrame % (Image.Width / (int)FrameSize.X);
                //int maxY = Image.Height/(int)FrameSize.Y;
                //int maxX = Image.Width / (int)FrameSize.X;
                _currentFrame = new Rectangle((int)(xFrame * FrameSize.X), (int)(yFrame * FrameSize.Y), (int)FrameSize.X, (int)FrameSize.Y);
                _currentIntFrame++;
                _currentIntFrame %= Frames;
            }
            Invalidate();
            base.Update(gameTime);
        }
        public void UpdateImg(GameTime gameTime)
        {
            
        }
    }
}
